package myServlet;

import Smt.ServerImp.BillServerImp;
import Smt.Util.Bill;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SuppressWarnings("serial")
public class BillUpdate extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		int id = 0;
		String ids = request.getParameter("id");
		if (ids != null) {
			id = Integer.parseInt(ids);
		}
		String prices = request.getParameter("price");
		double price = 0;
		if (prices != null) {
			price = Double.parseDouble(prices);
		}
		String unit = request.getParameter("unit");
		String numbers = request.getParameter("number");
		int number = 0;
		if (numbers != null) {
			number = Integer.parseInt(numbers);
		}
		String description = request.getParameter("description");
		String pay = request.getParameter("pay");
		String name = request.getParameter("name");
		int sno = Integer.parseInt(request.getParameter("s_name"));
		Bill b = new Bill(id, price, unit, number, name, description, sno, pay);
		BillServerImp bsi = new BillServerImp();
		if (bsi.update(b)) {
			int nums1 = Integer.parseInt(request.getParameter("num"));
			request.setAttribute("num", nums1);
			request.getRequestDispatcher("BillRead").forward(request, response);
		} else {
			request.setAttribute("error", "更新信息有误，我也不知道是什么问题！");
			response.sendRedirect("BillRead");
		}
	}
}
